#ifndef _SOCKET_BASE_H
#define _SOCKET_BASE_H

#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/epoll.h> 
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <dirent.h>
#include <assert.h>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <list>
#include <map>

using namespace std;


/* 重定义一些数据类型 */
typedef void                      VOID;
typedef char                      CHAR;
typedef unsigned char             BYTE;
typedef unsigned int              BOOL;
typedef unsigned short            WORD16;
typedef unsigned int              WORD32;
typedef int                       SWORD32;
typedef unsigned long             ULONG;
typedef stringstream              SSTREAM;
typedef ostringstream             OSTREAM;
typedef istringstream             ISTREAM;
typedef std::string               STRCPP;
typedef std::vector<string>       STRVEC;
typedef std::map<string, string>  STRMAP;
typedef std::map<string, string>::iterator STRMAPIT;
/*------------------------------------------------*/
typedef int                       SOCKFD;
typedef struct sockaddr_in        SOCKADDRIN;
typedef struct sockaddr           SOCKADDR;
typedef struct epoll_event        EPEVENT;
/*------------------------------------------------*/
typedef ofstream                  OFSTREAM;
typedef ifstream                  IFSTREAM;
typedef int                       FILEFD;
typedef struct stat               FILEST;
typedef struct dirent             DIRENT;
/*------------------------------------------------*/
typedef pthread_t                 THREAD;
typedef pthread_mutex_t           TMUTEX;
typedef pthread_cond_t            TCONDT;
/*------------------------------------------------*/
typedef pid_t                     PID;
typedef int                       PSTAT;
/*------------------------------------------------*/
typedef time_t                    TIMET;
typedef tm                        TIMETM;
typedef struct timeval            TIMEVAL;

/*------------------------------------------------*/
#define TRUE                      (1)
#define FALSE                     (0)
#define FDREAD                    0
#define FDWRITE                   1
#define STDIN                     0
#define STDOUT                    1
#define INVALIDFD                 ((SOCKFD)-1)
/*------------------------------------------------*/


// 定义socket地址类 封装socket ip port
class CSktAddr
{
public:
	CSktAddr();
	STRCPP addr();
	STRCPP portstr();
    VOID   set_noblock();
    VOID   setaddr(SOCKFD fd, STRCPP ip, WORD16 port);

public:
	SOCKFD fd;    // socket
	STRCPP ip;    // ip地址
	WORD16 port;  // 端口号
};


// 定义socket请求基类
class CSktReq
{
public:
	CSktReq();
	virtual ~CSktReq();

    SOCKFD selffd()		  {return local_addr_.fd;}
    SOCKFD peerfd()		  {return peer_addr_.fd;}
	
    VOID   closefd(SOCKFD fd);
	VOID   closeself();
	VOID   closepeer();

    VOID   set_local_addr(SOCKFD fd);
    VOID   set_peer_addr(SOCKFD fd);

    BOOL readinfo(STRCPP& buffer);
    BOOL sendinfo(const STRCPP& buffer);

	virtual VOID* clone() = 0;
	virtual BOOL  initialize(STRCPP svrip, WORD16 svrport, BOOL block) = 0;
	virtual BOOL  activate(BOOL block) = 0;
	virtual BOOL  receive() = 0;
	virtual BOOL  process() = 0;
protected:
    STRCPP   recv_buff_;  //接收数据的buffer
    STRCPP   send_buff_;  //发送数据的buffer
    CSktAddr local_addr_;  //本端地址
    CSktAddr peer_addr_;  //对端地址
};

typedef list<CSktReq*>			 REQUESTLIST;
typedef list<CSktReq*>::iterator REQUESTITER;
typedef map<SOCKFD, CSktReq*>    REQUESTMAP;


// 定义socket应用基类
class CSktApp
{
public:
	CSktApp(STRCPP ip, WORD16 port) : m_ip(ip), m_port(port), m_reqtype(NULL), m_reqblock(TRUE){}
	virtual ~CSktApp();
	virtual VOID working() = 0;
    virtual VOID setreq(CSktReq* req);
	virtual VOID startup();
protected:
    STRCPP     m_ip;
    WORD16     m_port;
	CSktReq*   m_reqtype;  // 请求原型 (使用原型模式)
	BOOL       m_reqblock; // socket是否为阻塞模式
};

STRCPP get_current_time();
STRCPP getgmttime(TIMET timestamp);
BOOL   getline(STRCPP& buffer, STRCPP& line);
BOOL   getsize(STRCPP& buffer, STRCPP& str, ULONG size);
VOID   del_crlf(STRCPP& buffer);
VOID   strsplit(const STRCPP& str, STRCPP sep, STRVEC& strvec);



#endif
